var util = require('../utils/util');
var daoUtil = require('../utils/daoUtil');
var stGpRt = require('../class/stockGroupRate');
var hisPic = require('../class/stock_history_price');

module.exports = {

    create: function (params, cb) {
        var validate = daoUtil.validate(['rate','stockGroupId', 'positionRate'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            var cls = new stGpRt();
            cls.init(params);
            models.stock_group_rate.create(cls.getParams(), function (err, data) {
                if (err || !data) {
                    console.log(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    //获取组合收益当前记录最大时间
    findMaxDateByGroupId:function (params,cb) {
        var validate = daoUtil.validate(['stockGroupId'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            models.stock_group_rate.findOne({'stockGroupId':params.stockGroupId}).max('countDate').groupBy('stockGroupId').exec(function (err, data) {
                if (err ) {
                    console.log(err);
                    cb(util.getSysError());
                }else{

                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    list: function (params, cb) {
        var validate = daoUtil.validate(['stockGroupId'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            models.stock_group_rate.find({'stockGroupId':params.stockGroupId,sort:'countDate asc'}).exec(function (err, data) {
                if (err || !data) {
                    console.log(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    delete:function (rowIds, cb) {
        daoUtil.proxy(function(models){
            models.stock_group_rate.destroy().exec(function (err, result) {
                if (err) {
                    console.log(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(result));
                }
            });
        },cb);
    }

};